﻿using System;
using System.Collections.Generic;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Terraria.Localization;
using Terraria.Social;

namespace Terraria.Achievements
{
	// Token: 0x020004A0 RID: 1184
	[JsonObject(MemberSerialization.OptIn)]
	public class Achievement
	{
		// Token: 0x14000045 RID: 69
		// (add) Token: 0x060027A7 RID: 10151 RVA: 0x00494D38 File Offset: 0x00492F38
		// (remove) Token: 0x060027A8 RID: 10152 RVA: 0x00494D70 File Offset: 0x00492F70
		public event Achievement.AchievementCompleted OnCompleted;

		// Token: 0x170002D7 RID: 727
		// (get) Token: 0x060027A9 RID: 10153 RVA: 0x0001BD1D File Offset: 0x00019F1D
		public AchievementCategory Category
		{
			get
			{
				return this._category;
			}
		}

		// Token: 0x170002D8 RID: 728
		// (get) Token: 0x060027AA RID: 10154 RVA: 0x0001BD25 File Offset: 0x00019F25
		public bool HasTracker
		{
			get
			{
				return this._tracker != null;
			}
		}

		// Token: 0x170002D9 RID: 729
		// (get) Token: 0x060027AB RID: 10155 RVA: 0x0001BD30 File Offset: 0x00019F30
		public bool IsCompleted
		{
			get
			{
				return this._completedCount == this._conditions.Count;
			}
		}

		// Token: 0x060027AC RID: 10156 RVA: 0x0001BD45 File Offset: 0x00019F45
		public IAchievementTracker GetTracker()
		{
			return this._tracker;
		}

		// Token: 0x060027AD RID: 10157 RVA: 0x00494DA8 File Offset: 0x00492FA8
		public Achievement(string name)
		{
			this.Name = name;
			this.FriendlyName = Language.GetText("Achievements." + name + "_Name");
			this.Description = Language.GetText("Achievements." + name + "_Description");
		}

		// Token: 0x060027AE RID: 10158 RVA: 0x00494E18 File Offset: 0x00493018
		public void ClearProgress()
		{
			this._completedCount = 0;
			foreach (KeyValuePair<string, AchievementCondition> keyValuePair in this._conditions)
			{
				keyValuePair.Value.Clear();
			}
			if (this._tracker != null)
			{
				this._tracker.Clear();
			}
		}

		// Token: 0x060027AF RID: 10159 RVA: 0x00494E8C File Offset: 0x0049308C
		public void Load(Dictionary<string, JObject> conditions)
		{
			foreach (KeyValuePair<string, JObject> keyValuePair in conditions)
			{
				AchievementCondition achievementCondition;
				if (this._conditions.TryGetValue(keyValuePair.Key, out achievementCondition))
				{
					achievementCondition.Load(keyValuePair.Value);
					if (achievementCondition.IsCompleted)
					{
						this._completedCount++;
					}
				}
			}
			if (this._tracker != null)
			{
				this._tracker.Load();
			}
		}

		// Token: 0x060027B0 RID: 10160 RVA: 0x0001BD4D File Offset: 0x00019F4D
		public void AddCondition(AchievementCondition condition)
		{
			this._conditions[condition.Name] = condition;
			condition.OnComplete += this.OnConditionComplete;
		}

		// Token: 0x060027B1 RID: 10161 RVA: 0x00494F20 File Offset: 0x00493120
		private void OnConditionComplete(AchievementCondition condition)
		{
			this._completedCount++;
			if (this._completedCount == this._conditions.Count)
			{
				if (this._tracker == null && SocialAPI.Achievements != null)
				{
					SocialAPI.Achievements.CompleteAchievement(this.Name);
				}
				if (this.OnCompleted != null)
				{
					this.OnCompleted(this);
				}
			}
		}

		// Token: 0x060027B2 RID: 10162 RVA: 0x0001BD73 File Offset: 0x00019F73
		private void UseTracker(IAchievementTracker tracker)
		{
			tracker.ReportAs("STAT_" + this.Name);
			this._tracker = tracker;
		}

		// Token: 0x060027B3 RID: 10163 RVA: 0x0001BD92 File Offset: 0x00019F92
		public void UseTrackerFromCondition(string conditionName)
		{
			this.UseTracker(this.GetConditionTracker(conditionName));
		}

		// Token: 0x060027B4 RID: 10164 RVA: 0x00494F84 File Offset: 0x00493184
		public void UseConditionsCompletedTracker()
		{
			ConditionsCompletedTracker conditionsCompletedTracker = new ConditionsCompletedTracker();
			foreach (KeyValuePair<string, AchievementCondition> keyValuePair in this._conditions)
			{
				conditionsCompletedTracker.AddCondition(keyValuePair.Value);
			}
			this.UseTracker(conditionsCompletedTracker);
		}

		// Token: 0x060027B5 RID: 10165 RVA: 0x00494FEC File Offset: 0x004931EC
		public void UseConditionsCompletedTracker(params string[] conditions)
		{
			ConditionsCompletedTracker conditionsCompletedTracker = new ConditionsCompletedTracker();
			foreach (string key in conditions)
			{
				conditionsCompletedTracker.AddCondition(this._conditions[key]);
			}
			this.UseTracker(conditionsCompletedTracker);
		}

		// Token: 0x060027B6 RID: 10166 RVA: 0x0001BDA1 File Offset: 0x00019FA1
		public void ClearTracker()
		{
			this._tracker = null;
		}

		// Token: 0x060027B7 RID: 10167 RVA: 0x0001BDAA File Offset: 0x00019FAA
		private IAchievementTracker GetConditionTracker(string name)
		{
			return this._conditions[name].GetAchievementTracker();
		}

		// Token: 0x060027B8 RID: 10168 RVA: 0x0049502C File Offset: 0x0049322C
		public void AddConditions(params AchievementCondition[] conditions)
		{
			for (int i = 0; i < conditions.Length; i++)
			{
				this.AddCondition(conditions[i]);
			}
		}

		// Token: 0x060027B9 RID: 10169 RVA: 0x00495050 File Offset: 0x00493250
		public AchievementCondition GetCondition(string conditionName)
		{
			AchievementCondition result;
			if (this._conditions.TryGetValue(conditionName, out result))
			{
				return result;
			}
			return null;
		}

		// Token: 0x060027BA RID: 10170 RVA: 0x0001BDBD File Offset: 0x00019FBD
		public void SetCategory(AchievementCategory category)
		{
			this._category = category;
		}

		// Token: 0x040043DA RID: 17370
		private static int _totalAchievements;

		// Token: 0x040043DB RID: 17371
		public readonly string Name;

		// Token: 0x040043DC RID: 17372
		public readonly LocalizedText FriendlyName;

		// Token: 0x040043DD RID: 17373
		public readonly LocalizedText Description;

		// Token: 0x040043DE RID: 17374
		public readonly int Id = Achievement._totalAchievements++;

		// Token: 0x040043DF RID: 17375
		private AchievementCategory _category;

		// Token: 0x040043E0 RID: 17376
		private IAchievementTracker _tracker;

		// Token: 0x040043E1 RID: 17377
		[JsonProperty("Conditions")]
		private Dictionary<string, AchievementCondition> _conditions = new Dictionary<string, AchievementCondition>();

		// Token: 0x040043E2 RID: 17378
		private int _completedCount;

		// Token: 0x020004A1 RID: 1185
		// (Invoke) Token: 0x060027BC RID: 10172
		public delegate void AchievementCompleted(Achievement achievement);
	}
}
